<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['pm:cattask:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column prop="id" label="编号" align="center" width="100" />
      <el-table-column prop="name" label="名称" min-width="160" />
      <el-table-column prop="remark" label="描述" min-width="180" />
      <el-table-column prop="enable" label="启用" align="center" width="120">
        <template #default="scope">
          <el-switch
            v-model="scope.row.enable"
            :active-value="1"
            :inactive-value="0"
            :disabled="!checkPermi(['pm:cattask:update'])"
            @change="() => handleEnableChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" width="120" />
      <el-table-column prop="createTime" label="创建时间" :formatter="dateFormatter" width="180" align="center" />
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openAssign(scope.row.id)"
            v-hasPermi="['pm:cattask:assignUsers']"
            >分配用户</el-button
          >
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pm:cattask:update']"
            >编辑</el-button
          >
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['pm:cattask:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <CatTaskForm ref="formRef" @success="getList" />
  <AssignUsersForm ref="assignRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { checkPermi } from '@/utils/permission'
import * as CatTaskApi from '@/api/pm/cattask'
import CatTaskForm from './CatTaskForm.vue'
import AssignUsersForm from './AssignUsersForm.vue'

defineOptions({ name: 'PmCatTask' })

const message = useMessage()

const loading = ref(true)
const total = ref(0)
const list = ref<any[]>([])
const queryFormRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined as string | undefined
})

const getList = async () => {
  loading.value = true
  try {
    const data: any = await CatTaskApi.getCatTaskPage({
      page: queryParams.pageNo,
      limit: queryParams.pageSize,
      keyWord: queryParams.name
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields?.()
  handleQuery()
}

const formRef = ref()
const openForm = (mode: 'create' | 'update', id?: number) => {
  formRef.value.open(mode, id)
}

const assignRef = ref()
const openAssign = (id: number) => {
  assignRef.value.open(id)
}

const handleEnableChange = async (row: any) => {
  const previous = row.enable === 1 ? 0 : 1
  try {
    await CatTaskApi.editCatTask({ id: row.id, enable: row.enable })
    message.success('修改成功')
  } catch {
    row.enable = previous
  }
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await CatTaskApi.deleteCatTask(id)
    message.success('删除成功')
    await getList()
  } catch {}
}

onMounted(() => getList())
</script>
